<?php
/**
 * Created by PhpStorm.
 * User: Sing
 * Date: 16/10/25
 * Time: 17:29
 */

class memory_driver_redis
{
    var $enable;
    var $obj;

    function init($config) {
        if(!empty($config['server'])) {
            try {
                $this->obj = new Redis();
                if($config['pconnect']) {
                    $connect = @$this->obj->pconnect($config['server'], $config['port']);
                } else {
                    $connect = @$this->obj->connect($config['server'], $config['port']);
                }
            } catch (RedisException $e) {
            }
            $this->enable = $connect ? true : false;
            if($this->enable) {
                if($config['requirepass']) {
                    $this->obj->auth($config['requirepass']);
                }
                @$this->obj->setOption(Redis::OPT_SERIALIZER, $config['serializer']);
            }
        }
    }

    function &instance() {
        static $object;
        if(empty($object)) {
            $object = new memory_driver_redis();
            $object->init(getglobal('config/memory/redis'));
        }
        return $object;
    }
    function zadd($key , $value){
        if(!isset($value[0]) || !isset($value[1])) return false;
        $v1 = $value[0];
        $v2 = $value[1];
        $this->obj->zadd($key , $v1 , $v2);
        return true;
    }
    function zrem($key , $value){
        return $this->obj->zrem($key , $value);
    }
    function zcount($key , $value){
        if(!isset($value[0])) return 0;
        $v1 = $value[0];
        $v2 = isset($value[1])?$value[1]:-1;
        return $this->obj->zcount($key , $v1 , $v2);
    }
    function zcard($key){
        return $this->obj->zcard($key);
    }
    function zrange($key, $value){
        if(!isset($value[0])) return array();
        $start = $value[0];
        $len = isset($value[1])?$value[1]:-1;
        return $this->obj->zrange($key, $start ,$len);
    }
    function zscore($key , $value){
        return $this->obj->zscore($key , $value);
    }
    function zremrangebyscore($key, $value){
        if(!isset($value[0])) return false;
        $start = $value[0];
        $len = isset($value[1])?$value[1]:-1;
        return $this->obj->zremrangebyscore($key, $start ,$len);
    }
    function zremrangebyrank($key, $value){
        if(!isset($value[0])) return false;
        $start = $value[0];
        $len = isset($value[1])?$value[1]:-1;
        return $this->obj->zremrangebyrank($key, $start ,$len);
    }
    function get($key) {
        if(is_array($key)) {
            return $this->getMulti($key);
        }
        return $this->obj->get($key);
    }

    function getMulti($keys) {
        $result = $this->obj->getMultiple($keys);
        $newresult = array();
        $index = 0;
        foreach($keys as $key) {
            if($result[$index] !== false) {
                $newresult[$key] = $result[$index];
            }
            $index++;
        }
        unset($result);
        return $newresult;
    }

    function select($db=0) {
        return $this->obj->select($db);
    }

    function set($key, $value, $ttl = 0) {
        if($ttl) {
            return $this->obj->setex($key, $ttl, $value);
        } else {
            return $this->obj->set($key, $value);
        }
    }

    function rm($key) {
        return $this->obj->delete($key);
    }

    function setMulti($arr, $ttl=0) {
        if(!is_array($arr)) {
            return FALSE;
        }
        foreach($arr as $key => $v) {
            $this->set($key, $v, $ttl);
        }
        return TRUE;
    }

    function inc($key, $step = 1) {
        return $this->obj->incr($key, $step);
    }

    function dec($key, $step = 1) {
        return $this->obj->decr($key, $step);
    }

    function getSet($key, $value) {
        return $this->obj->getSet($key, $value);
    }

    function sADD($key, $value) {
        return $this->obj->sADD($key, $value);
    }

    function sRemove($key, $value) {
        return $this->obj->sRemove($key, $value);
    }

    function sMembers($key) {
        return $this->obj->sMembers($key);
    }

    function sIsMember($key, $member) {
        return $this->obj->sismember($key, $member);
    }

    function keys($key) {
        return $this->obj->keys($key);
    }

    function expire($key, $second){
        return $this->obj->expire($key, $second);
    }

    function sCard($key) {
        return $this->obj->sCard($key);
    }

    function hSet($key, $field, $value) {
        return $this->obj->hSet($key, $field, $value);
    }

    function hDel($key, $field) {
        return $this->obj->hDel($key, $field);
    }

    function hLen($key) {
        return $this->obj->hLen($key);
    }

    function hVals($key) {
        return $this->obj->hVals($key);
    }

    function hIncrBy($key, $field, $incr){
        return $this->obj->hIncrBy($key, $field, $incr);
    }

    function hGetAll($key) {
        return $this->obj->hGetAll($key);
    }

    function sort($key, $opt) {
        return $this->obj->sort($key, $opt);
    }

    function exists($key) {
        return $this->obj->exists($key);
    }
    function flushdb(){
        return $this->obj->flushdb();
    }
    function clear() {
        return $this->obj->flushAll();
    }
}

?>